package servlets;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Adresse;
import model.Termin;
import model.Termin.TerminArt;
import service.TerminplanerConnectionPool;
import service.TerminplanerService;
import service.TokenStore;

@WebServlet("/AddTerminServlet")
public class AddTerminServlet extends HttpServlet{
  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    request.getSession().setAttribute("token", TokenStore.getInstance().createToken());
    request.getRequestDispatcher("/WEB-INF/jsps/addTermin.jsp").forward(request, response);
  }
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String requestguid = request.getParameter("token");
    if (!TokenStore.getInstance().verifyToken(requestguid)) {
      request.setAttribute("exception", "DoubleSubmit wurde abgefangen");
      request.getRequestDispatcher("/WEB-INF/jsps/error.jsp").forward(request, response);
      return;
    }
    
    Connection con = null;
    Termin termin = null;
    String terminid = null;
    
    String thema = request.getParameter("thema");
    String terminanfang = request.getParameter("terminanfang");
    String terminende = request.getParameter("terminende");
    String terminart = request.getParameter("terminart");
    String prioritaet = request.getParameter("prioritaet");
    String strasse = request.getParameter("strasse");
    String plz = request.getParameter("plz");
    String ort = request.getParameter("ort");
    String land = request.getParameter("land");
    String beschreibung = request.getParameter("beschreibung");
    terminid = request.getParameter("terminid");
    
    try {
      con  = TerminplanerConnectionPool.getInstanz().getConnection();
      Adresse adresse = new Adresse(ort, plz, strasse, land);
      termin = new Termin(thema, beschreibung, TerminplanerService.getInstanz().parseStringtoDateWithTime(terminanfang), TerminplanerService.getInstanz().parseStringtoDateWithTime(terminende), TerminArt.getTerminArt(terminart), new Integer(prioritaet), adresse);
      if(terminid != null && terminid.trim().length() != 0)
      { termin.setID(Long.valueOf(terminid));
         adresse.setID(Long.valueOf(terminid));
      }
      TerminplanerService.getInstanz().saveTermin(con, termin);
    } catch (Exception e) {
      request.setAttribute("exception", e.getMessage());
      request.getRequestDispatcher("/WEB-INF/jsps/error.jsp").forward(request, response);
      e.printStackTrace();
      return;
    }
    finally{
      TerminplanerConnectionPool.getInstanz().saveClose(con);
    }
    
    response.sendRedirect("TerminplanerServlet");
    
    
  }
  
  
}
